package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Tag;
import com.hs.service.TagService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("tag")
public class TagController {
    @Autowired
    private TagService tagService;
    @PostMapping
    public R addTag(@RequestBody Tag tag) {
        Integer i = tagService.addTag(tag);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("失败");
        }
    }
    @DeleteMapping("{id}")
    public R deleteTag(@PathVariable("id") Integer id) {
        Integer i = tagService.deleteTag(id);
        if (i > 0) {
            return R.ok();
        }else{
            return R.fail("失败");
        }
    }
    @PutMapping
    public R updateTag(@RequestBody Tag tag) {
        Integer i = tagService.updateTag(tag);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("失败");
        }
    }
    @GetMapping("{id}")
    public R getTagById(@PathVariable("id") Integer id) {
        Tag tagById = tagService.getTagById(id);
        return R.ok(tagById);
    }
    @GetMapping("page")
    public R getTagByPage(@RequestParam(defaultValue = "1",required = false) Integer pageNum,
                          @RequestParam(defaultValue = "5",required = false) Integer pageSize) {
        PageInfo<Tag> tagByPage = tagService.getTagByPage(pageNum, pageSize);
        return R.ok(tagByPage);
    }
}
